package org.example.spring_blog.service;

import lombok.extern.slf4j.Slf4j;
import org.example.spring_blog.mapper.BlogMapper;
import org.example.spring_blog.mapper.UserInfoMapper;
import org.example.spring_blog.model.BlogInfo;
import org.example.spring_blog.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class UserService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private BlogMapper blogMapper;

    public UserInfo selectByName(String username) {

        return userInfoMapper.selectByName(username);
    }

    public UserInfo selectById(Integer userId) {
        return userInfoMapper.selectById(userId);
    }

    public UserInfo getAuthorInfo(Integer blogId) {
        //1. 根据博客ID, 获取作者ID
        //2. 根据作者ID, 获取作者信息
        BlogInfo blogInfo = blogMapper.selectById(blogId);
        if (blogInfo==null && blogInfo.getUserId()<=0){
            log.error("博客不存在或博客作者信息不合法, blogId:{}", blogId);
            return null;
        }
        return userInfoMapper.selectById(blogInfo.getUserId());


    }
}
